package com.wx.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.wx.mall.feign.LoginLogFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author LiRui
 */
@Component
public class TokenFilter extends ZuulFilter {
    @Autowired
    private LoginLogFeignClient logFeign;

    /**
     * 过滤类型
     * pre: 路由前
     * routing: 路由时
     * post: 路由后
     * error: 错误时
     *
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 是否过滤
     *
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {

//        RequestContext context = RequestContext.getCurrentContext();
//        HttpServletRequest request = context.getRequest();
////        String token = request.getParameter("ip");
////        if (!StringUtils.isEmpty(token)) {
//        request.setAttribute("ip","119.139.196.128");
//            logFeign.add(request);
////        }
//        context.setSendZuulResponse(true); // 路由
//        context.setResponseStatusCode(200);
//        context.set("isSuccess", true);

//        context.setSendZuulResponse(false); // 不对其进行路由
//        context.setResponseStatusCode(401);
//        context.setResponseBody("token is empty...");

//        RequestContext context = RequestContext.getCurrentContext();
//        HttpServletRequest request = context.getRequest();
//        String token = request.getParameter("token");
//        if(StringUtils.isEmpty(token)){
//            context.setSendZuulResponse(false); // 不对其进行路由
//            context.setResponseStatusCode(401);
//            context.setResponseBody("token is empty...");
//        } else {
//            context.setSendZuulResponse(true); // 路由
//            context.setResponseStatusCode(200);
//            context.set("isSuccess",true);
//        }

        return null;
    }
}
